package cn.dengta.webapp.trade.entity;

import java.util.function.Function;
import javax.annotation.Nullable;

import me.codeplayer.util.NumberUtil;
import me.codeplayer.util.X;

public enum UserStatField {
	/** 注册时间 */
	addTime(UserStat.ADD_TIME, -1, UserStat::getAddTime, "注册时间", "小时"),
	/** 累计收益总额度（以0.01积分为1单位额度） */
	incomeSum(UserStat.INCOME_SUM, 0, UserStat::getIncomeSum, "累计收益", "积分", null, UserStatField::multi100),
	/** 累计充值总额度（以 分 为单位额度） */
	rechargeSum(UserStat.RECHARGE_SUM, 1, UserStat::getRechargeSum, "累计充值", "元", null, i -> NumberUtil.getInteger(UserStatField.multi100(i), null)),
	/** 速配次数 */
	//	speedDatingTimes(UserStat.SPEED_DATING_TIMES, -1, UserStat::getSpeedDatingTimes, "速配次数", "次"),
	/** 搭讪次数 */
	greetTimes(UserStat.GREET_TIMES, 1, UserStat::getGreetTimes, "搭讪次数", "次"),
	/** 礼物消费总额 */
	giftCostSum(UserStat.GIFT_COST_SUM, 1, UserStat::getGiftCostSum, "礼物消费", "钻石"),
	/** 视频消费总额 */
	videoCostSum(UserStat.VIDEO_COST_SUM, 1, UserStat::getVideoCostSum, "视频消费", "钻石"),
	/** 语音消费总额 */
	audioCostSum(UserStat.AUDIO_COST_SUM, 1, UserStat::getAudioCostSum, "语音消费", "钻石"),
	/** 文字消费总额 */
	msgCostSum(UserStat.MSG_COST_SUM, 1, UserStat::getMsgCostSum, "文字消费", "钻石"),
	/** 钻石账户余额 */
	activeCoin(UserStat.ACTIVE_COIN, 1, UserStat::getActiveCoin, "钻石余额", "钻石"),
	/** 登录天数 */
	loginDayCount(UserStat.LOGIN_DAY_COUNT, 1, UserStat::getLoginDayCount, "累计使用APP天数", "天"),
	/** 总的登录次数（APP启动时会主动上报一次） */
	loginTimes(UserStat.LOGIN_TIMES, 1, UserStat::getLoginTimes, "累计启动APP次数", "次"),
	/** 平均视频时长（单位：s） */
	videoTimeAvg(UserStat.VIDEO_TIMES, 0, UserStat::getVideoTimes, "平均视频", "秒", "(video_time_sum / GREATEST(video_times, 1))", null),
	/** 平均语音时长（单位：s） */
	audioTimesAvg(UserStat.AUDIO_TIMES, 0, UserStat::getAudioTimes, "平均语音", "秒", "(audio_time_sum / GREATEST(audio_times, 1))", null),
	/** 文本消息收益总额度（以0.01积分为1单位额度） */
	msgIncomeSum(UserStat.MSG_INCOME_SUM, 0, UserStat::getMsgIncomeSum, "文本占比", "%", "(msg_income_sum * 100 / GREATEST(income_sum, 1))", UserLayerConfig.castLong),
	/** 视频收益总额度（以0.01积分为1单位额度） */
	videoIncomeSum(UserStat.VIDEO_INCOME_SUM, 0, UserStat::getVideoIncomeSum, "视频占比", "%", "(video_income_sum * 100 / GREATEST(income_sum, 1))", UserLayerConfig.castLong),
	/** 语音收益总额度（以0.01积分为1单位额度） */
	audioIncomeSum(UserStat.AUDIO_INCOME_SUM, 0, UserStat::getAudioIncomeSum, "语音占比", "%", "(audio_income_sum * 100 / GREATEST(income_sum, 1))", UserLayerConfig.castLong),
	/** 礼物收益总额度（以0.01积分为1单位额度） */
	giftIncomeSum(UserStat.GIFT_INCOME_SUM, 0, UserStat::getGiftIncomeSum, "礼物占比", "%", "(gift_income_sum * 100 / GREATEST(income_sum, 1))", UserLayerConfig.castLong),
	/** 回复率（%） */
	replyRate(UserStat.REPLY_RATE, -1, UserStat::getReplyRate, "回复率", "%"),
	/** 被回复率（%） */
	repliedRate(UserStat.REPLIED_RATE, 0, UserStat::getRepliedRate, "被回复率", "%"),
	/** 所属推广员ID */
	inviteUserId(UserStat.INVITE_USER_ID, 0, UserStat::getInviteUserId, "所属推广员", "", UserLayerConfig.castLong, false),
	/** 注册国家 */
	regCountry(UserStat.REG_COUNTRY, -1, UserStat::getRegCountry, "注册国家", "", false),
	/** 用户ID */
	userId(UserStat.ID, 0, UserStat::getId, "用户ID", "", false);

	public final String column;
	public final String label;
	public final String unit;
	/** -1=不限；0=仅限女性；1=仅限男性 */
	public final int gender;
	public final String formula;
	/** 配置值的转换器 */
	@Nullable
	public final Function<Object, Comparable<?>> converter;
	public boolean range = true;
	public final Function<? super UserStat, Comparable<?>> getter;

	UserStatField(String column, int gender, Function<? super UserStat, Comparable<?>> getter, String label, String unit, String formula, @Nullable Function<Object, Comparable<?>> converter) {
		this.column = column;
		this.label = label;
		this.unit = unit;
		this.gender = gender;
		this.formula = formula;
		this.getter = getter;
		this.converter = converter;
	}

	public String columnSql() {
		return formula == null ? column : formula;
	}

	UserStatField(String column, int gender, Function<? super UserStat, Comparable<?>> getter, String label, String unit) {
		this(column, gender, getter, label, unit, null, null);
	}

	UserStatField(String column, int gender, Function<? super UserStat, Comparable<?>> getter, String label, String unit, boolean range) {
		this(column, gender, getter, label, unit, null, null);
		this.range = range;
	}

	UserStatField(String column, int gender, Function<? super UserStat, Comparable<?>> getter, String label, String unit, Function<Object, Comparable<?>> converter, boolean range) {
		this(column, gender, getter, label, unit, null, converter);
		this.range = range;
	}

	static Long multi100(Object o) {
		if (o == null) {
			return null;
		}
		return NumberUtil.getBigDecimal(o).movePointRight(2).longValue();
	}

	public Comparable<?> clip(Object o) {
		return X.castType(converter == null ? o : converter.apply(o));
	}

}
